package com.freud.logic.business.controller;

import java.util.HashMap;
import java.util.Map;

import oracle.jrockit.jfr.tools.ConCatRepository;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.freud.framework.base.BaseController;
import com.freud.framework.base.BaseRequest;
import com.freud.logic.business.bean.Car;
import com.freud.logic.business.model.GoodCondition;
import com.freud.logic.business.service.CarService;
import com.freud.logic.business.service.GoodService;

@Controller
@RequestMapping("/car")
public class CarController extends BaseController {

	@Autowired
	private CarService carService;

	@Autowired
	private GoodService goodService;

	@RequestMapping(value = "/index")
	public String index() {
		return "car.index";
	}

	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> list(BaseRequest baseRequest) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", carService.getAll(buildRowBounds()).size());
		result.put("rows", carService.getAll(buildRowBounds(baseRequest)));
		return result;
	}

	@RequestMapping(value = "/add")
	public ModelAndView add() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("car.add");
		return mav;
	}

	@RequestMapping(value = "/save")
	public String save(Car car) {
		carService.insert(car);
		return "redirect:/car/index";
	}

	@RequestMapping(value = "/edit")
	public ModelAndView edit(int id) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("car.edit");
		mav.addObject("car", carService.getById(id));
		return mav;
	}

	@RequestMapping(value = "/send")
	public ModelAndView send(int id) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("car.send");
		mav.addObject("car", carService.getById(id));
		mav.addObject("goods",
				goodService.getAll(new RowBounds(), new GoodCondition()));
		return mav;
	}

	@RequestMapping(value = "/send/save")
	public String send(Car car) {
		carService.sendCar(car);
		return "redirect:/car/index";
	}

	@RequestMapping(value = "/back")
	public String back(int id) {
		carService.backCar(id);
		return "redirect:/car/index";
	}

	@RequestMapping(value = "/update")
	public String update(Car car) {
		carService.update(car);
		return "redirect:/car/index";
	}

	@RequestMapping(value = "/map")
	public String map(int id) {
		Car car = carService.getById(id);
		return "redirect:/good/map?id=" + car.getGoodId();
	}

	@RequestMapping(value = "/delete")
	public String deleter(Integer id) {
		carService.delete(id);
		return "redirect:/car/index";
	}
}
